#include "calque.h"
#include "GraphicsArea/drawingarea.h"
using namespace UiCalques;

//************************************************************* Constructor

Calque::Calque(QDockWidget *dockCalques){
    contentDockWidgetCalques = new QWidget();

    drawingCalque= NULL;

    uiCalques = new CalquesClass();
    uiCalques->setupUi(contentDockWidgetCalques);

    dockCalques->setWidget(contentDockWidgetCalques);

    QObject::connect(uiCalques->sliderOpacite , SIGNAL(valueChanged(int)), uiCalques->spinBoxOpacite, SLOT(setValue(int)));
    QObject::connect(uiCalques->spinBoxOpacite, SIGNAL(valueChanged(int)), uiCalques->sliderOpacite , SLOT(setValue(int)));

    QObject::connect(uiCalques->sliderOpacite , SIGNAL(valueChanged(int)), this, SLOT(changeValeurOpacite()));
    QObject::connect(uiCalques->spinBoxOpacite, SIGNAL(valueChanged(int)), this, SLOT(changeValeurOpacite()));

    QObject::connect(uiCalques->actionNewCalque, SIGNAL(triggered()), this, SLOT(nouveauCalque()));
    QObject::connect(uiCalques->actionDeleteCalque, SIGNAL(triggered()), this, SLOT(deleteCalque()));
    QObject::connect(uiCalques->actionFusionUnion, SIGNAL(triggered()), this, SLOT(fusionUnion()));
    QObject::connect(uiCalques->actionFusionAdapte, SIGNAL(triggered()), this, SLOT(fusionAdapte()));
    QObject::connect(uiCalques->actionFusionIntersection, SIGNAL(triggered()), this, SLOT(fusionIntersection()));
}

//************************************************************* Slots

void Calque::changeValeurOpacite(){
    if(drawingCalque!=NULL)
        drawingCalque->getCurrentLayer()->changeValeurOpacite(uiCalques->spinBoxOpacite->value());
}

void Calque::nouveauCalque(){
    drawingCalque->addLayerAfterCurrentLayer();
    updateCalque(drawingCalque);
}

void Calque::deleteCalque(){
    drawingCalque->removeCurrentLayer();
    updateCalque(drawingCalque);
}

void Calque::fusionUnion(){
    drawingCalque->fusionVisibleLayers(ResizeToUnionLayers);
    updateCalque(drawingCalque);
}

void Calque::fusionAdapte(){
    drawingCalque->fusionVisibleLayers(SizeImage);
    updateCalque(drawingCalque);
}

void Calque::fusionIntersection(){
    drawingCalque->fusionVisibleLayers(CropToIntersectionLayers);
    updateCalque(drawingCalque);
}

//************************************************************* Creation of miniCalque

// Update the display whith a new drawing
void Calque::updateCalque(DrawingArea* drawing){
    // Initialisation of the scrollBarArea
    razCalque();

    uiCalques->actionNewCalque->setEnabled(true);

    uiCalques->actionFusionCalque->setEnabled(true);

    std::list <Layer*> listLayers = drawing->getLayers();
    Layer* tmpLayer;

    drawingCalque = drawing;

    // Adding all the miniCalques
    if(!listLayers.empty()){
        tmpLayer = listLayers.front();
        while(!listLayers.empty()){
            MiniCalque* miniCalque;
            if(tmpLayer == drawing->getCurrentLayer()){
                miniCalque = new MiniCalque(drawing,tmpLayer,true);
            }else{
                miniCalque = new MiniCalque(drawing,tmpLayer,false);
            }

            createContentOfMiniCalque(miniCalque,tmpLayer);

            listObjectInMiniCalque.push_front(miniCalque);
            listLayers.pop_front();
            tmpLayer = listLayers.front();
        }
    }
    if(drawingCalque->getCurrentLayer() != NULL){
        uiCalques->sliderOpacite->setValue(drawingCalque->getCurrentLayer()->opacity()*100);
    }
    if(drawing->getLayers().size() >= 2){
         uiCalques->actionDeleteCalque->setEnabled(true);
     }else{
         uiCalques->actionDeleteCalque->setEnabled(false);
    }
}

// Create the content of the miniCalque
void Calque::createContentOfMiniCalque(MiniCalque* miniCalque, Layer* layer){

    uiCalques->createNewMiniCalqueGraphisme(miniCalque);
    listObjectInMiniCalque.push_front(miniCalque);

    QPixmap pixmap_img = pixmap_img.fromImage(*layer->getImage());
    pixmap_img = pixmap_img.scaled(QSize(40, 30));
    // Set the differents label and options
    uiCalques->labelCalque->setPixmap(pixmap_img);

    uiCalques->labelTitreCalque->setText(layer->getNomLayer());

    if (layer->isVisible()){
         uiCalques->checkBoxCalqueVisible->setChecked(true);
    }else{
         uiCalques->checkBoxCalqueVisible->setChecked(false);
    }
    QObject::connect(uiCalques->checkBoxCalqueVisible,SIGNAL(stateChanged(int)),miniCalque,SLOT(changeEtatVisible()));
}

// Raz the display of calque
void Calque::razCalque(){
    // This method delete the scrollBarArea and recreate it
    uiCalques->razCalqueGraphisme();
    // Removing the different miniCalques
    while(!listObjectInMiniCalque.empty()){
        uiCalques->layoutZoneCalque->removeWidget(listObjectInMiniCalque.front());
        listObjectInMiniCalque.remove(listObjectInMiniCalque.front());
    }
}
